import { Component, OnInit, Input } from "@angular/core";
import {
  FormBuilder,
  FormGroup,
  Validators,
  FormControl
} from "@angular/forms";
import { SelfCheckBean } from "../bean/self-check.bean";
import { Config } from "../../../../service/config";
import { NzModalService, NzModalSubject } from "ng-zorro-antd";
import { Router, ActivatedRoute, ParamMap } from "@angular/router";
import { HttpServcie } from "../../../../service/http.service";
import { NzNotificationService } from "ng-zorro-antd";
import * as moment from "moment";
import { CommFuntion } from "../../../../service/commFunction";
@Component({
  selector: "self-check-detail",
  templateUrl: "./self-check-detail.component.html",
  styleUrls: ["./self-check-detail.component.scss"]
})
export class SelfCheckDetailComponent implements OnInit {
  selfCheckBean: any = new SelfCheckBean();
  isDisable: boolean = true;
  picList = [];
  config = Config;
  isSpinning: boolean = false;
  showBean: any = {};
  elevatorType: any;
  constructor(
    private fb: FormBuilder,
    private modalService: NzModalService,
    private router: Router,
    private route: ActivatedRoute,
    private httpServcie: HttpServcie,
    private _notification: NzNotificationService,
    private currentModal: NzModalSubject,
    private commFuntion: CommFuntion
  ) {}

  ngOnInit() {
    this.route.paramMap.subscribe(data => {
      this.elevatorType = data.get("elevatorType");
      //查询电梯的信息
      this.httpServcie.querySelfCheckDetail(data.get("id")).subscribe(data => {
        if (data.data) {
          this.selfCheckBean = this.commFuntion.convertBean(data.data);
          this.selfCheckBean.maintUserSignTime = this.transformTime(
            this.selfCheckBean.maintUserSignTime
          );
          this.selfCheckBean.safeUserSignTime = this.transformTime(
            this.selfCheckBean.safeUserSignTime
          );
          this.selfCheckBean.checkTime = this.transformTime(
            this.selfCheckBean.checkTime
          );
        }
      });
    });
  }

  //返回
  toBack() {
    window.history.back();
  }

  transformTime(value) {
    if (!value) {
      return null;
    }
    let values = value + "";
    let date = values.substring(0, 8);
    let time = values.substring(8, 14);
    let dates = moment(date).format("YYYY-MM-DD");
    let times =
      time.substring(0, 2) +
      ":" +
      time.substring(2, 4) +
      ":" +
      time.substring(4, 6);

    return dates + " " + times;
  }

  showLargeImage(titlepic, contentpic, footerpic, img) {
    this.picList = [];
    this.picList.push(img);
    this.currentModal = this.modalService.open({
      title: titlepic,
      content: contentpic,
      footer: footerpic,
      maskClosable: false,
      width: 800,
      style: {
        top: "20px"
      },
      onOk() {
        console.log("Click ok");
      }
    });
  }

  download(url, name) {
    let link = document.createElement("a");
    let body = document.querySelector("body");

    console.log("haha"+ this.config.IP + this.config.API.getUpload + url);
    this.getBlob(this.config.IP + this.config.API.getUpload + url).then(
      blob => {
        link.href = window.URL.createObjectURL(blob);
        link.download = name;
        // fix Firefox
        link.style.display = "none";
        body.appendChild(link);
        link.click();
        body.removeChild(link);
        window.URL.revokeObjectURL(link.href);
      }
    );
  }
  getBlob(url) {
    return new Promise(resolve => {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.responseType = "blob";
      xhr.onload = () => {
        if (xhr.status === 200) {
          resolve(xhr.response);
        }
      };
      xhr.send();
    });
  }
}
